package dao.impl;

import dao.UserDao;
/*import org.junit.Test;*/
import pojo.User;
import util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 用户类Dao接口实现
 *
 * @author Jamin
 */
public class UserDaoImpl implements UserDao {
    PreparedStatement ps = null;
    Connection con = null;
    ResultSet rs = null;

    @Override
    public int userRegister(User user) {
        int a = 0;
        try {
            con = BaseDao.getConnection();
            String s = "insert into E_USER values(?,sequence_uid.nextval,?,?,?,?,?,?,?,0,sysdate)";
            ps = con.prepareStatement(s);
            ps.setString(1, user.getuIcon());
            ps.setString(2, user.getuAccount());
            ps.setString(3, user.getuPassword());
            ps.setString(4, user.getuNickName());
            ps.setInt(5, user.getuGender());
            ps.setString(6, user.getuPhoneNumber());
            ps.setString(7, user.getuEmail());
            ps.setString(8, user.getuAddress());
            a = ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }

    /**
     * 用户登录
     *
     * @param s1 用户账号
     * @param s2 用户密码
     * @return User对象
     */
    @Override
    public User userLogin(String s1, String s2) {
        User user = new User();
        try {
            con = BaseDao.getConnection();
            String s = "select * from e_user where u_account=? and u_password=?";
            ps = con.prepareStatement(s);
            ps.setString(1, s1);
            ps.setString(2, s2);
            rs = ps.executeQuery();
            while (rs.next()) {
                user.setuIcon(rs.getString("u_icon"));
                user.setuID(rs.getInt("u_id"));
                user.setuAccount(rs.getString("u_account"));
                user.setuPassword(rs.getString("u_password"));
                user.setuNickName(rs.getString("u_nickname"));
                user.setuGender(rs.getInt("u_gender"));
                user.setuPhoneNumber(rs.getString("u_phonenumber"));
                user.setuEmail(rs.getString("u_email"));
                user.setuIntegral(rs.getInt("u_Integral"));
                user.setuRegdate(rs.getDate("u_regdate"));
                user.setuAddress(rs.getString("u_address"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return user;
    }

    @Override
    public int queryOne(String email) {
        int uId=0;
        try {
            con = BaseDao.getConnection();
            String s = "select * from e_user where U_EMAIL=?" ;
            ps = con.prepareStatement(s);
            ps.setString(1,email);
            rs = ps.executeQuery();
            while (rs.next()) {

                uId=rs.getInt("u_id");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return uId;
    }

    @Override
    public int queryIntegral(int uId) {
        int integral=0;
        try {
            con = BaseDao.getConnection();
            String s = "select * from e_user where U_ID=?" ;
            ps = con.prepareStatement(s);
            ps.setInt(1,uId);
            rs = ps.executeQuery();
            while (rs.next()) {

                integral=rs.getInt("U_INTEGRAL");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return integral;
    }

    @Override
    public int reduceIntegral(int integral,int uId) {
        int finaIntegral=this.queryIntegral(uId)-integral;
        int result=0;
        try {
            con = BaseDao.getConnection();
            String s = "UPDATE e_user SET U_INTEGRAL="+finaIntegral+" WHERE U_ID="+uId ;
            System.out.println(finaIntegral);
            ps = con.prepareStatement(s);
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return result;
    }

    /**
     *通过账号查询用户id
     * @param account
     * @return
     */
    @Override
    public int searchID(String account) {
        int a=0;
        try {
            con = BaseDao.getConnection();
            String s = "select * from e_user where u_account=?";
            ps = con.prepareStatement(s);
            ps.setString(1, account);

            rs = ps.executeQuery();
            while (rs.next()) {
                User user=new User();
            user.setuID(rs.getInt("u_id"));
            a=user.getuID();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }


    /**
     * 查找用户
     * @param s1   账号
     * @param s2    手机号
     * @return      查找条数
     */
    @Override
    public int searchUser(String s1, String s2) {
        int a=0;
        try {
            con = BaseDao.getConnection();
            String s = "select count(*) from e_user where u_account=? and U_PHONENUMBER=?";
            ps = con.prepareStatement(s);
            ps.setString(1, s1);
            ps.setString(2, s2);
            rs = ps.executeQuery();
            while (rs.next()) {
            a=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }

    /**
     * 重置密码
     * @param s1 新密码
     * @param s2    账号
     * @return
     */

    @Override
    public int resetPwd(String s1, String s2) {
        int a=0;
        try {
            con = BaseDao.getConnection();
            String s = "update E_USER set U_PASSWORD=? where U_ACCOUNT=?";
            ps = con.prepareStatement(s);
            ps.setString(1, s1);
            ps.setString(2, s2);
           a=ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }

    /**
     * 查询账号是否存在
     * @param s1 账号
     * @return
     */
    @Override
    public int searchUserByAc(String s1) {
        int a=0;
        try {
            con = BaseDao.getConnection();
            String s = "select count(*) from e_user where u_account=?";
            ps = con.prepareStatement(s);
            ps.setString(1, s1);
            rs = ps.executeQuery();
            while (rs.next()) {
                a=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.release(rs, ps, con);
        }
        return a;
    }


}




